library(data.table)
library(lubridate)
library(stringi)
library(stringr)
library(ggplot2)
library(ggthemes)
library(ggrepel)
library(scales)
library(showtext)

# Declare working directory beforehand in an environment variable
# IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH = "path_to_your_folder"
# with the aid of usethis::edit_r_environ()
# Restart R session for the changes to take effect
path <- Sys.getenv("IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH")
setwd(path)

# Load objects with case-instance-side-outcome-level data
load("data/crimea_case_outcomes_side_instance.rdata")
load("data/krasnodar_case_outcomes_side_instance.rdata")

# Load yearly win rates for entire Russia as prepared
# in 1d_prepare_all_russia_data.r
all_russia_government_yearly_win_rate <- fread("data/all_russia_government_yearly_win_rate.csv")
all_russia_local_entity_yearly_win_rate <- fread("data/all_russia_local_entity_yearly_win_rate.csv")

# Font for plotting
font_name <- "Arial"
showtext_auto()

##############################
# Aggregate data at case level (with first instance taking precedence)

# Involved pension fund dummy
crimea_case_outcomes_side_instance[, involved_pension_fund := max(federal_agency_type == "pf", na.rm = T), by = "caseid"]
crimea_case_outcomes_side_instance[ !is.finite(involved_pension_fund), involved_pension_fund := 0]

krasnodar_case_outcomes_side_instance[, involved_pension_fund := max(federal_agency_type == "pf", na.rm = T), by = "caseid"]
krasnodar_case_outcomes_side_instance[ !is.finite(involved_pension_fund), involved_pension_fund := 0]

cols_to_keep <- c("caseid", "caseTypeCode", "caseCategoryUnified", "caseCategoryRedux", "caseCategory_government", "caseCategory_private", "year", "year_factor", "petty_case", "countDocumentsByCourt", "lncountDocumentsByCourt", "days_elapsed", "claimSum_deflated", "claimSum_decile", "recoverySum_deflated", "was_appealed", "registrationDate", "date", "dispute_type", "local_entities_only", "involved_pension_fund", names(crimea_case_outcomes_side_instance)[grepl("judge|government|agency|local_entity", names(crimea_case_outcomes_side_instance))])

crimea_cases <- crimea_case_outcomes_side_instance[, lapply(.SD, function(x) x[[1]]), by = "caseid", .SDcols = cols_to_keep ]
crimea_cases[, c("government", "federal_agency", "regional_agency", "municipal_agency", "local_entity") := NULL ]

cols_to_keep <- c("caseid", "caseTypeCode", "caseCategoryUnified", "caseCategoryRedux", "caseCategory_government", "caseCategory_private", "year", "year_factor", "petty_case", "countDocumentsByCourt", "lncountDocumentsByCourt", "days_elapsed", "claimSum_deflated", "claimSum_decile", "recoverySum_deflated", "was_appealed", "registrationDate", "date", "dispute_type", "local_entities_only", "involved_pension_fund", names(krasnodar_case_outcomes_side_instance)[grepl("judge|government|agency|local_entity", names(krasnodar_case_outcomes_side_instance))])

krasnodar_cases <- krasnodar_case_outcomes_side_instance[, lapply(.SD, function(x) x[[1]]), by = "caseid", .SDcols = cols_to_keep ]
krasnodar_cases[, c("government", "federal_agency", "regional_agency", "municipal_agency", "local_entity") := NULL ]


##############################
# TABLE 1. Government victory shares in Government vs. Private disputes or Private disputes where one of the parties is not local, Crimea and Sevastopol, Krasnodar, and Russian arbitrazh courts of first instance, 2014–2019
# NB: the code to replicate all-Russia win rates by agency and instance level is not reported

# Crimea
## First instance
crimea_government_win_rates_byagency_first_instance <- crimea_cases[ year >= 2014 & involved_government == 1, list(
		region = "Crimea",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
crimea_government_win_rates_byagency_first_instance <- melt(crimea_government_win_rates_byagency_first_instance, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_first_instance[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_first_instance <- dcast(crimea_government_win_rates_byagency_first_instance, variable ~ region + instanceLevel)

## Appeals
crimea_government_win_rates_byagency_final <- crimea_cases[ year >= 2014 & involved_government == 1, list(
		region = "Crimea",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
crimea_government_win_rates_byagency_final <- melt(crimea_government_win_rates_byagency_final, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_final[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_final <- dcast(crimea_government_win_rates_byagency_final, variable ~ region + instanceLevel)

# Krasnodar
## First instance
krasnodar_government_win_rates_byagency_first_instance <- krasnodar_cases[ year >= 2014 & involved_government == 1, list(
		region = "krasnodar",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
krasnodar_government_win_rates_byagency_first_instance <- melt(krasnodar_government_win_rates_byagency_first_instance, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_first_instance[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_first_instance <- dcast(krasnodar_government_win_rates_byagency_first_instance, variable ~ region + instanceLevel)

## Appeals
krasnodar_government_win_rates_byagency_final <- krasnodar_cases[ year >= 2014 & involved_government == 1, list(
		region = "krasnodar",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
krasnodar_government_win_rates_byagency_final <- melt(krasnodar_government_win_rates_byagency_final, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_final[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_final <- dcast(krasnodar_government_win_rates_byagency_final, variable ~ region + instanceLevel)

# Construct final object
government_win_rates_byagency <- cbind(crimea_government_win_rates_byagency_first_instance, crimea_government_win_rates_byagency_final, krasnodar_government_win_rates_byagency_first_instance, krasnodar_government_win_rates_byagency_final)

# Export the table
fwrite(government_win_rates_byagency, file = "tables/table1_government_win_rates_byagency.csv")

# Local entity total win rates by
# region and agency level

# Crimea
## First instance
crimea_local_entity_win_rates_byagency_first_instance <- crimea_cases[ year >= 2014 & involved_government == 0 & local_entities_only == 0, list(
		region = "Crimea",
		instanceLevel = "1_first_instance",
		`01_local_entity_win_first` = mean(local_entity_win_first, na.rm = T),
		`02_local_entity_win_first_semean` = sd(local_entity_win_first, na.rm = T)/sqrt(sum(!is.na(local_entity_win_first)))
	)
]
# Reshape
crimea_local_entity_win_rates_byagency_first_instance <- melt(crimea_local_entity_win_rates_byagency_first_instance, id.vars = c("region", "instanceLevel"))
crimea_local_entity_win_rates_byagency_first_instance[, value := round(100*value, 2)]
crimea_local_entity_win_rates_byagency_first_instance <- dcast(crimea_local_entity_win_rates_byagency_first_instance, variable ~ region + instanceLevel)

## Appeals
crimea_local_entity_win_rates_byagency_final <- crimea_cases[ year >= 2014 & involved_government == 0 & local_entities_only == 0, list(
		region = "Crimea",
		instanceLevel = "2_appeals",
		`01_local_entity_win_final` = mean(local_entity_win_final, na.rm = T),
		`02_local_entity_win_final_semean` = sd(local_entity_win_final, na.rm = T)/sqrt(sum(!is.na(local_entity_win_final)))
	)
]
# Reshape
crimea_local_entity_win_rates_byagency_final <- melt(crimea_local_entity_win_rates_byagency_final, id.vars = c("region", "instanceLevel"))
crimea_local_entity_win_rates_byagency_final[, value := round(100*value, 2)]
crimea_local_entity_win_rates_byagency_final <- dcast(crimea_local_entity_win_rates_byagency_final, variable ~ region + instanceLevel)

# Krasnodar
## First instance
krasnodar_local_entity_win_rates_byagency_first_instance <- krasnodar_cases[ year >= 2014 & involved_government == 0 & local_entities_only == 0, list(
		region = "krasnodar",
		instanceLevel = "1_first_instance",
		`01_local_entity_win_first` = mean(local_entity_win_first, na.rm = T),
		`02_local_entity_win_first_semean` = sd(local_entity_win_first, na.rm = T)/sqrt(sum(!is.na(local_entity_win_first)))
	)
]
# Reshape
krasnodar_local_entity_win_rates_byagency_first_instance <- melt(krasnodar_local_entity_win_rates_byagency_first_instance, id.vars = c("region", "instanceLevel"))
krasnodar_local_entity_win_rates_byagency_first_instance[, value := round(100*value, 2)]
krasnodar_local_entity_win_rates_byagency_first_instance <- dcast(krasnodar_local_entity_win_rates_byagency_first_instance, variable ~ region + instanceLevel)

## Appeals
krasnodar_local_entity_win_rates_byagency_final <- krasnodar_cases[ year >= 2014 & involved_government == 0 & local_entities_only == 0, list(
		region = "krasnodar",
		instanceLevel = "2_appeals",
		`01_local_entity_win_final` = mean(local_entity_win_final, na.rm = T),
		`02_local_entity_win_final_semean` = sd(local_entity_win_final, na.rm = T)/sqrt(sum(!is.na(local_entity_win_final)))
	)
]
# Reshape
krasnodar_local_entity_win_rates_byagency_final <- melt(krasnodar_local_entity_win_rates_byagency_final, id.vars = c("region", "instanceLevel"))
krasnodar_local_entity_win_rates_byagency_final[, value := round(100*value, 2)]
krasnodar_local_entity_win_rates_byagency_final <- dcast(krasnodar_local_entity_win_rates_byagency_final, variable ~ region + instanceLevel)

# Construct final object
local_entity_win_rates_byagency <- cbind(crimea_local_entity_win_rates_byagency_first_instance, crimea_local_entity_win_rates_byagency_final, krasnodar_local_entity_win_rates_byagency_first_instance, krasnodar_local_entity_win_rates_byagency_final)

# Export the table
fwrite(local_entity_win_rates_byagency, file = "tables/table1_local_entity_win_rates_byagency.csv")

##############################
# TABLE S4. Government victory shares in Government vs. Private disputes or Private disputes where one of the parties is not local, Crimea and Sevastopol, Krasnodar, and Russian arbitrazh courts of first instance, 2014–2019: Robustness to sample composition

# Government total win rates by
# region and agency level excluding
# Pension fund cases

# Crimea
## First instance
crimea_government_win_rates_byagency_excl_pension_first_instance <- crimea_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0, list(
		region = "Crimea",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
crimea_government_win_rates_byagency_excl_pension_first_instance <- melt(crimea_government_win_rates_byagency_excl_pension_first_instance, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_excl_pension_first_instance[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_excl_pension_first_instance <- dcast(crimea_government_win_rates_byagency_excl_pension_first_instance, variable ~ region + instanceLevel)

## Appeals
crimea_government_win_rates_byagency_excl_pension_final <- crimea_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0, list(
		region = "Crimea",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
crimea_government_win_rates_byagency_excl_pension_final <- melt(crimea_government_win_rates_byagency_excl_pension_final, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_excl_pension_final[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_excl_pension_final <- dcast(crimea_government_win_rates_byagency_excl_pension_final, variable ~ region + instanceLevel)

# Krasnodar
## First instance
krasnodar_government_win_rates_byagency_excl_pension_first_instance <- krasnodar_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0, list(
		region = "krasnodar",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
krasnodar_government_win_rates_byagency_excl_pension_first_instance <- melt(krasnodar_government_win_rates_byagency_excl_pension_first_instance, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_excl_pension_first_instance[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_excl_pension_first_instance <- dcast(krasnodar_government_win_rates_byagency_excl_pension_first_instance, variable ~ region + instanceLevel)

## Appeals
krasnodar_government_win_rates_byagency_excl_pension_final <- krasnodar_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0, list(
		region = "krasnodar",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
krasnodar_government_win_rates_byagency_excl_pension_final <- melt(krasnodar_government_win_rates_byagency_excl_pension_final, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_excl_pension_final[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_excl_pension_final <- dcast(krasnodar_government_win_rates_byagency_excl_pension_final, variable ~ region + instanceLevel)

# Construct final object
government_win_rates_byagency_excl_pension <- cbind(crimea_government_win_rates_byagency_excl_pension_first_instance, crimea_government_win_rates_byagency_excl_pension_final, krasnodar_government_win_rates_byagency_excl_pension_first_instance, krasnodar_government_win_rates_byagency_excl_pension_final)

# Export the table
fwrite(government_win_rates_byagency_excl_pension, file = "tables/tableS4_government_win_rates_byagency_excl_pension.csv")

# Government total win rates by
# region and agency level considering
# only civil cases

# Crimea
## First instance
crimea_government_win_rates_byagency_only_civil_first_instance <- crimea_cases[ year >= 2014 & involved_government == 1 & caseTypeCode == "civil", list(
		region = "Crimea",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
crimea_government_win_rates_byagency_only_civil_first_instance <- melt(crimea_government_win_rates_byagency_only_civil_first_instance, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_only_civil_first_instance[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_only_civil_first_instance <- dcast(crimea_government_win_rates_byagency_only_civil_first_instance, variable ~ region + instanceLevel)

## Appeals
crimea_government_win_rates_byagency_only_civil_final <- crimea_cases[ year >= 2014 & involved_government == 1 & caseTypeCode == "civil", list(
		region = "Crimea",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
crimea_government_win_rates_byagency_only_civil_final <- melt(crimea_government_win_rates_byagency_only_civil_final, id.vars = c("region", "instanceLevel"))
crimea_government_win_rates_byagency_only_civil_final[, value := round(100*value, 2)]
crimea_government_win_rates_byagency_only_civil_final <- dcast(crimea_government_win_rates_byagency_only_civil_final, variable ~ region + instanceLevel)

# krasnodar
## First instance
krasnodar_government_win_rates_byagency_only_civil_first_instance <- krasnodar_cases[ year >= 2014 & involved_government == 1 & caseTypeCode == "civil", list(
		region = "krasnodar",
		instanceLevel = "1_first_instance",
		`01_government_win_first` = mean(government_win_first, na.rm = T),
		`02_government_win_first_semean` = sd(government_win_first, na.rm = T)/sqrt(sum(!is.na(government_win_first))),
		`03_federal_agency_win_first` = mean(federal_agency_win_first, na.rm = T),
		`04_federal_agency_win_first_semean` = sd(federal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_first))),
		`05_regional_agency_win_first` = mean(regional_agency_win_first, na.rm = T),
		`06_regional_agency_win_first_semean` = sd(regional_agency_win_first, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_first))),
		`07_municipal_agency_win_first` = mean(municipal_agency_win_first, na.rm = T),
		`08_municipal_agency_win_first_semean` = sd(municipal_agency_win_first, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_first)))

	)
]
# Reshape
krasnodar_government_win_rates_byagency_only_civil_first_instance <- melt(krasnodar_government_win_rates_byagency_only_civil_first_instance, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_only_civil_first_instance[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_only_civil_first_instance <- dcast(krasnodar_government_win_rates_byagency_only_civil_first_instance, variable ~ region + instanceLevel)

## Appeals
krasnodar_government_win_rates_byagency_only_civil_final <- krasnodar_cases[ year >= 2014 & involved_government == 1 & caseTypeCode == "civil", list(
		region = "krasnodar",
		instanceLevel = "2_appeals",
		`01_government_win_final` = mean(government_win_final, na.rm = T),
		`02_government_win_final_semean` = sd(government_win_final, na.rm = T)/sqrt(sum(!is.na(government_win_final))),
		`03_federal_agency_win_final` = mean(federal_agency_win_final, na.rm = T),
		`04_federal_agency_win_final_semean` = sd(federal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(federal_agency_win_final))),
		`05_regional_agency_win_final` = mean(regional_agency_win_final, na.rm = T),
		`06_regional_agency_win_final_semean` = sd(regional_agency_win_final, na.rm = T)/sqrt(sum(!is.na(regional_agency_win_final))),
		`07_municipal_agency_win_final` = mean(municipal_agency_win_final, na.rm = T),
		`08_municipal_agency_win_final_semean` = sd(municipal_agency_win_final, na.rm = T)/sqrt(sum(!is.na(municipal_agency_win_final)))
	)
]
# Reshape
krasnodar_government_win_rates_byagency_only_civil_final <- melt(krasnodar_government_win_rates_byagency_only_civil_final, id.vars = c("region", "instanceLevel"))
krasnodar_government_win_rates_byagency_only_civil_final[, value := round(100*value, 2)]
krasnodar_government_win_rates_byagency_only_civil_final <- dcast(krasnodar_government_win_rates_byagency_only_civil_final, variable ~ region + instanceLevel)

# Construct final object
government_win_rates_byagency_only_civil <- cbind(crimea_government_win_rates_byagency_only_civil_first_instance, crimea_government_win_rates_byagency_only_civil_final, krasnodar_government_win_rates_byagency_only_civil_first_instance, krasnodar_government_win_rates_byagency_only_civil_final)

# Export the table
fwrite(government_win_rates_byagency_only_civil, file = "tables/tableS4_government_win_rates_byagency_only_civil.csv")

##############################
# Government yearly win rates by
# region: overall and by case category

crimea_government_win_rates_bygroup <- crimea_cases[ involved_government == 1, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("petty_case", "caseTypeCode", "year")]
crimea_government_win_rates_overall <- crimea_cases[ involved_government == 1, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]
crimea_government_win_rates_overall_excl_pension <- crimea_cases[ involved_government == 1 & involved_pension_fund == 0, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]
crimea_government_win_rates_overall_only_civil <- crimea_cases[ involved_government == 1 & caseTypeCode == "civil", list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]

krasnodar_government_win_rates_bygroup <- krasnodar_cases[ involved_government == 1, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("petty_case", "caseTypeCode", "year")]
krasnodar_government_win_rates_overall <- krasnodar_cases[ involved_government == 1, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]
krasnodar_government_win_rates_overall_excl_pension <- krasnodar_cases[ involved_government == 1 & involved_pension_fund == 0, list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]
krasnodar_government_win_rates_overall_only_civil <- krasnodar_cases[ involved_government == 1 & caseTypeCode == "civil", list(government_win = mean(government_win_first, na.rm = T), semean = sd(government_win_first, na.rm = T)/sqrt(.N)), by = c("year")]

# Compute CIs
crimea_government_win_rates_bygroup[, lower := government_win - 1.96*semean ]
crimea_government_win_rates_bygroup[, upper := government_win + 1.96*semean ]
crimea_government_win_rates_overall[, lower := government_win - 1.96*semean ]
crimea_government_win_rates_overall[, upper := government_win + 1.96*semean ]
crimea_government_win_rates_overall_excl_pension[, lower := government_win - 1.96*semean ]
crimea_government_win_rates_overall_excl_pension[, upper := government_win + 1.96*semean ]
crimea_government_win_rates_overall_only_civil[, lower := government_win - 1.96*semean ]
crimea_government_win_rates_overall_only_civil[, upper := government_win + 1.96*semean ]

krasnodar_government_win_rates_bygroup[, lower := government_win - 1.96*semean ]
krasnodar_government_win_rates_bygroup[, upper := government_win + 1.96*semean ]
krasnodar_government_win_rates_overall[, lower := government_win - 1.96*semean ]
krasnodar_government_win_rates_overall[, upper := government_win + 1.96*semean ]
krasnodar_government_win_rates_overall_excl_pension[, lower := government_win - 1.96*semean ]
krasnodar_government_win_rates_overall_excl_pension[, upper := government_win + 1.96*semean ]
krasnodar_government_win_rates_overall_only_civil[, lower := government_win - 1.96*semean ]
krasnodar_government_win_rates_overall_only_civil[, upper := government_win + 1.96*semean ]

# To readable levels
crimea_government_win_rates_bygroup[, petty_case := factor(petty_case)]
levels(crimea_government_win_rates_bygroup$petty_case) <- c("Non-petty cases", "Petty cases")
crimea_government_win_rates_bygroup[, caseTypeCode := droplevels(caseTypeCode)]
levels(crimea_government_win_rates_bygroup$caseTypeCode) <- c("Administrative cases", "Civil cases")

krasnodar_government_win_rates_bygroup[, petty_case := factor(petty_case)]
levels(krasnodar_government_win_rates_bygroup$petty_case) <- c("Non-petty cases", "Petty cases")
krasnodar_government_win_rates_bygroup[, caseTypeCode := droplevels(caseTypeCode)]
levels(krasnodar_government_win_rates_bygroup$caseTypeCode) <- c("Administrative cases", "Civil cases")

# Row-bind to one object
crimea_government_win_rates_overall[, region := "Crimea" ]
krasnodar_government_win_rates_overall[, region := "Krasnodar" ]
crimea_government_win_rates_overall_excl_pension[, region := "Crimea" ]
krasnodar_government_win_rates_overall_excl_pension[, region := "Krasnodar" ]
crimea_government_win_rates_overall_only_civil[, region := "Crimea" ]
krasnodar_government_win_rates_overall_only_civil[, region := "Krasnodar" ]

government_win_rates_overall <- rbind(crimea_government_win_rates_overall, krasnodar_government_win_rates_overall)
government_win_rates_overall_excl_pension <- rbind(crimea_government_win_rates_overall_excl_pension, krasnodar_government_win_rates_overall_excl_pension)
government_win_rates_overall_only_civil <- rbind(crimea_government_win_rates_overall_only_civil, krasnodar_government_win_rates_overall_only_civil)

crimea_government_win_rates_bygroup[, region := "Crimea" ]
krasnodar_government_win_rates_bygroup[, region := "Krasnodar" ]
government_win_rates_bygroup <- rbind(crimea_government_win_rates_bygroup, krasnodar_government_win_rates_bygroup)

# Add all-Russia figures to the overall plot
setnames(all_russia_government_yearly_win_rate, c("RegistrationYear", "win"), c("year", "government_win"))

all_russia_government_yearly_win_rate[, lower := government_win - 1.96*win_sd ]
all_russia_government_yearly_win_rate[, upper := government_win + 1.96*win_sd ]
all_russia_government_yearly_win_rate[, region := "Russia"]

government_win_rates_overall <- rbind(government_win_rates_overall, all_russia_government_yearly_win_rate, fill = T)

##############################
# FIGURE 2. Government victory shares in Government vs. Private disputes, Crimea and Sevastopol, Krasnodar, and Russian arbitrazh courts of first instance in 2012–2019

government_win_rates_overall_plot <- ggplot(aes(x = year, y = government_win, color = region, label = round(100*government_win, 1)), data = government_win_rates_overall) +
	geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey80", alpha = .3, linetype = 0) +
	geom_line(aes(linetype = region)) +
	geom_point() +
	geom_text_repel(size = 5, family = font_name, show.legend = F) +
	labs(y = "Share of Government vs. Private cases won by Government", x = "", fill = "", color = "", linetype = "", shape = "") +
	scale_x_continuous(expand = c(0.01, 0), n.breaks = 10) +
	scale_y_continuous(label = function(x) scales::percent(x, accuracy = 1)) +
	scale_colour_manual(values = c("black", "grey50", "grey30")) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 12), legend.text=element_text(size = 14))
ggsave(government_win_rates_overall_plot, file = "figures/figure2_government_win_rates_overall_plot.pdf", device = cairo_pdf, width = 8.75, height = 5)

##############################
# FIGURE S3: Government victory shares in Government vs. Private disputes, Crimea and Sevastopol, Krasnodar, and Russian arbitrazh courts of first instance in 2012–2019: Robustness to sample composition

# Excluding pension cases
government_win_rates_overall_excl_pension_plot <- ggplot(aes(x = year, y = government_win, color = region, label = round(100*government_win, 1)), data = government_win_rates_overall_excl_pension) +
	geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey80", alpha = .3, linetype = 0) +
	geom_line(aes(linetype = region)) +
	geom_point() +
	geom_text_repel(size = 5, family = font_name, show.legend = F) +
	labs(y = "Share of Government vs. Private cases won by Government", x = "", fill = "", color = "", linetype = "", shape = "") +
	scale_x_continuous(expand = c(0.01, 0), n.breaks = 10) +
	scale_y_continuous(label = function(x) scales::percent(x, accuracy = 1)) +
	scale_colour_manual(values = c("black", "grey50", "grey30")) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 12), legend.text=element_text(size = 14))
ggsave(government_win_rates_overall_excl_pension_plot, file = "figures/figureS3_government_win_rates_overall_excl_pension_plot.pdf", device = cairo_pdf, width = 8.75, height = 5)

# Only civil cases
government_win_rates_overall_only_civil_plot <- ggplot(aes(x = year, y = government_win, color = region, label = round(100*government_win, 1)), data = government_win_rates_overall_only_civil) +
	geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey80", alpha = .3, linetype = 0) +
	geom_line(aes(linetype = region)) +
	geom_point() +
	geom_text_repel(size = 5, family = font_name, show.legend = F) +
	labs(y = "Share of Government vs. Private cases won by Government", x = "", fill = "", color = "", linetype = "", shape = "") +
	scale_x_continuous(expand = c(0.01, 0), n.breaks = 10) +
	scale_y_continuous(label = function(x) scales::percent(x, accuracy = 1)) +
	scale_colour_manual(values = c("black", "grey50", "grey30")) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 12), legend.text=element_text(size = 14))
ggsave(government_win_rates_overall_only_civil_plot, file = "figures/figureS3_government_win_rates_overall_only_civil_plot.pdf", device = cairo_pdf, width = 8.75, height = 5)


##############################
# Local entity yearly win rates by region

crimea_local_entity_win_rates_bygroup <- crimea_cases[ involved_government == 0 & local_entities_only == 0, list(local_entity_win = mean(local_entity_win_first, na.rm = T), semean = sd(local_entity_win_first, na.rm = T)/sqrt(.N)), by = c("year", "petty_case")]
crimea_local_entity_win_rates_overall <- crimea_cases[ involved_government == 0 & local_entities_only == 0, list(local_entity_win = mean(local_entity_win_first, na.rm = T), semean = sd(local_entity_win_first, na.rm = T)/sqrt(.N)), by = c("year")]

krasnodar_local_entity_win_rates_bygroup <- krasnodar_cases[ involved_government == 0 & local_entities_only == 0, list(local_entity_win = mean(local_entity_win_first, na.rm = T), semean = sd(local_entity_win_first, na.rm = T)/sqrt(.N)), by = c("year", "petty_case")]
krasnodar_local_entity_win_rates_overall <- krasnodar_cases[ involved_government == 0 & local_entities_only == 0, list(local_entity_win = mean(local_entity_win_first, na.rm = T), semean = sd(local_entity_win_first, na.rm = T)/sqrt(.N)), by = c("year")]

# Compute CIs
crimea_local_entity_win_rates_bygroup[, lower := local_entity_win - 1.96*semean ]
crimea_local_entity_win_rates_bygroup[, upper := local_entity_win + 1.96*semean ]
crimea_local_entity_win_rates_overall[, lower := local_entity_win - 1.96*semean ]
crimea_local_entity_win_rates_overall[, upper := local_entity_win + 1.96*semean ]

krasnodar_local_entity_win_rates_bygroup[, lower := local_entity_win - 1.96*semean ]
krasnodar_local_entity_win_rates_bygroup[, upper := local_entity_win + 1.96*semean ]
krasnodar_local_entity_win_rates_overall[, lower := local_entity_win - 1.96*semean ]
krasnodar_local_entity_win_rates_overall[, upper := local_entity_win + 1.96*semean ]

# To readable levels
crimea_local_entity_win_rates_bygroup[, petty_case := factor(petty_case)]
levels(crimea_local_entity_win_rates_bygroup$petty_case) <- c("Non-petty cases", "Petty cases")

krasnodar_local_entity_win_rates_bygroup[, petty_case := factor(petty_case)]
levels(krasnodar_local_entity_win_rates_bygroup$petty_case) <- c("Non-petty cases", "Petty cases")

# Row-bind to one object
crimea_local_entity_win_rates_bygroup[, region := "Crimea" ]
krasnodar_local_entity_win_rates_bygroup[, region := "Krasnodar" ]
local_entity_win_rates_bygroup <- rbind(crimea_local_entity_win_rates_bygroup, krasnodar_local_entity_win_rates_bygroup)

crimea_local_entity_win_rates_overall[, region := "Crimea" ]
krasnodar_local_entity_win_rates_overall[, region := "Krasnodar" ]
local_entity_win_rates_overall <- rbind(crimea_local_entity_win_rates_overall, krasnodar_local_entity_win_rates_overall)

# Add all-Russia figures to the overall plot
setnames(all_russia_local_entity_yearly_win_rate, c("RegistrationYear", "win"), c("year", "local_entity_win"))

all_russia_local_entity_yearly_win_rate[, lower := local_entity_win - 1.96*win_sd ]
all_russia_local_entity_yearly_win_rate[, upper := local_entity_win + 1.96*win_sd ]
all_russia_local_entity_yearly_win_rate[, region := "Russia"]

local_entity_win_rates_overall <- rbind(local_entity_win_rates_overall, all_russia_local_entity_yearly_win_rate, fill = T)

##############################
# FIGURE 3. Local entity victory shares in Private disputes where one of the parties is not local, Crimea and Sevastopol, Krasnodar, and Russian arbitrazh courts of first instance in 2012–2019

local_entity_win_rates_overall_plot <- ggplot(aes(x = year, y = local_entity_win, color = region, label = round(100*local_entity_win, 1)), data = local_entity_win_rates_overall) +
	geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey70", alpha = .3, linetype = 0) +
	geom_line(aes(linetype = region)) +
	geom_point() +
	geom_text_repel(size = 5, family = font_name, show.legend = F) +
	labs(y = "Share of Private cases where one of the parties is local\nwon by local entity", x = "", fill = "", color = "", linetype = "", shape = "") +
	scale_x_continuous(expand = c(0.01, 0), n.breaks = 10) +
	scale_y_continuous(label = function(x) scales::percent(x, accuracy = 1)) +
	scale_colour_manual(values = c("black", "grey50", "grey30")) +
	theme_minimal() +
	theme(legend.position = "bottom", text = element_text(family = font_name, size = 12), legend.text=element_text(size = 14))
ggsave(local_entity_win_rates_overall_plot, file = "figures/figure3_local_entity_win_rates_overall_plot.pdf", device = cairo_pdf, width = 8.75, height = 5)


##############################
# Selected two-proportions one-tailed
# z-test

# Observed government win rates in
# Crimea are greater than in Kransodar
# in courts of first instance & apellate
# courts

## Counts
crimea_count_won <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & government_win_final == 1])
crimea_count_total <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 ])

krasondar_count_won <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & government_win_final == 1 ])
krasondar_count_total <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 ])

## Test
prop.test(x = c(crimea_count_won, krasondar_count_won), n = c(crimea_count_total, krasondar_count_total),
	alternative = "greater", correct = F)
#X-squared = 390.72, df = 1, p-value < 2.2e-16
#alternative hypothesis: greater
#95 percent confidence interval:
# 0.06068805 1.00000000
#sample estimates:
#  prop 1   prop 2 
#0.808802 0.742896 

# Observed federal agency win rates in
# Crimea are greater than in Kransodar
# in courts of first instance & apellate
crimea_count_won <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & federal_agency_win_final == 1])
crimea_count_total <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & !is.na(federal_agency_win_final) ])

krasondar_count_won <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & federal_agency_win_final == 1 ])
krasondar_count_total <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & !is.na(federal_agency_win_final) ])

## Test
prop.test(x = c(crimea_count_won, krasondar_count_won), n = c(crimea_count_total, krasondar_count_total),
	alternative = "greater", correct = F)
#X-squared = 698.87, df = 1, p-value < 2.2e-16
#alternative hypothesis: greater
#95 percent confidence interval:
# 0.09049612 1.00000000
#sample estimates:
#   prop 1    prop 2 
#0.8870042 0.7912347 

# Observed regional agency win rates in
# Crimea are lower than in Kransodar
# in courts of first instance & apellate
crimea_count_won <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & regional_agency_win_final == 1])
crimea_count_total <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & !is.na(regional_agency_win_final) ])

krasondar_count_won <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & regional_agency_win_final == 1 ])
krasondar_count_total <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & !is.na(regional_agency_win_final) ])

## Test
prop.test(x = c(crimea_count_won, krasondar_count_won), n = c(crimea_count_total, krasondar_count_total),
	alternative = "less", correct = F)
#X-squared = 55.502, df = 1, p-value = 4.667e-14
#alternative hypothesis: less
#95 percent confidence interval:
# -1.00000000 -0.05560434
#sample estimates:
#   prop 1    prop 2 
#0.6286732 0.7000572 

# Observed municipal agency win rates in
# Crimea are greater than in Kransodar
# in courts of first instance & apellate
crimea_count_won <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & municipal_agency_win_final == 1])
crimea_count_total <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & !is.na(municipal_agency_win_final) ])

krasondar_count_won <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & municipal_agency_win_final == 1 ])
krasondar_count_total <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & !is.na(municipal_agency_win_final) ])

## Test
prop.test(x = c(crimea_count_won, krasondar_count_won), n = c(crimea_count_total, krasondar_count_total),
	alternative = "greater", correct = F)
#X-squared = 85.151, df = 1, p-value < 2.2e-16
#alternative hypothesis: greater
#95 percent confidence interval:
# 0.07900723 1.00000000
#sample estimates:
#   prop 1    prop 2 
#0.6702241 0.5747581 

# Observed government win rates in
# Crimea are greater than in Kransodar
# excluding Pension fund cases
# in courts of first instance & apellate

## Counts
crimea_count_won <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & government_win_final == 1 & involved_pension_fund == 0 ])
crimea_count_total <- nrow(crimea_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0 ])

krasondar_count_won <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & government_win_final == 1 & involved_pension_fund == 0 ])
krasondar_count_total <- nrow(krasnodar_cases[ year >= 2014 & involved_government == 1 & involved_pension_fund == 0 ])

## Test
prop.test(x = c(crimea_count_won, krasondar_count_won), n = c(crimea_count_total, krasondar_count_total),
	alternative = "greater", correct = F)
#X-squared = 25.345, df = 1, p-value = 2.397e-07
#alternative hypothesis: greater
#95 percent confidence interval:
# 0.01741567 1.00000000
#sample estimates:
#   prop 1    prop 2 
#0.6836374 0.6579197
